package com.nondo.dean.saprfc.service;

import com.nondo.dean.saprfc.common.SapUtil;
import com.nondo.dean.saprfc.domain.ProductBomStructure;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author xiaofan
 * @Description: 获取sap系统的bom结构数据
 * @throws
 * @date 2018-03-3014:31
 */
@Service
public class CrmProductBomService {
    /**
     * 获取BOM头数据
     * @param SapClient
     * @return
     */
    public ArrayList<ProductBomStructure> getBomHeadDate(String SapClient) {
        Map<String, ProductBomStructure> bomStructureMap = new HashMap<String, ProductBomStructure>();
        JCoFunction function = SapUtil.getFunction(SapClient, "Z_SD_BOM_TRANS_A");
        function.getImportParameterList().setValue("VTWEG", "2040");
        SapUtil.executeFunction(SapClient, function);
        JCoTable V_TABLE = function.getTableParameterList().getTable("EX_BOM_HEAD");
        for (int i = 0; i < V_TABLE.getNumRows(); i++) {
            //物料号
            String v_code = V_TABLE.getString("MATNR");
            //物料描述（短文本）
            String v_describe = V_TABLE.getString("MAKTX");
            //组件数量 (BTCI)
            String v_compQty = V_TABLE.getString("COMP_QTY").trim();
            //组件计量单位
            String v_compUnit = V_TABLE.getString("COMP_UNIT");
            //BOM 删除标志
            String v_bomDel = V_TABLE.getString("DELETE_IND");
            //删除标识
            String v_delete = "";
            //上级物料号
            String v_superiorCode = "";
            String v_num = "";

            v_compQty = "".equals(v_compQty) ? "0" : v_compQty;

            ProductBomStructure bean = new ProductBomStructure(v_code, v_describe, v_superiorCode, v_compUnit, v_compQty, v_bomDel, v_delete, v_num);
            bomStructureMap.put(v_code, bean);
            V_TABLE.nextRow();
        }
        return getBomChildDate(function,bomStructureMap);
    }

    /**
     *获取子结构
     * @param function
     * @param bomStructureMap
     * @return
     */
    public ArrayList<ProductBomStructure> getBomChildDate(JCoFunction function, Map<String, ProductBomStructure> bomStructureMap){
        JCoTable table=function.getTableParameterList().getTable("EX_BOM_ITEM");
        for (int i = 0; i < table.getNumRows(); i++) {
            //物料号
            String v_code=table.getString("MATNR");
            //物料描述（短文本）
            String v_describe=table.getString("MAKTX");
            //物料号
            String v_superiorCode=table.getString("P_MATNR");
            //组件数量 (BTCI)
            String v_compQty=table.getString("COMP_QTY").trim();
            //组件计量单位
            String v_compUnit=table.getString("COMP_UNIT");
            //删除标识
            String v_FLDELETE=table.getString("FLDELETE");
            //BOM 删除标志
            String v_DELETE_IND="";

            String v_Pprodno="";

            v_compQty="".equals(v_compQty)?"0":v_compQty;

            ProductBomStructure bean=new ProductBomStructure(v_code, v_describe, v_superiorCode, v_compQty, v_compUnit, v_DELETE_IND, v_FLDELETE, v_Pprodno);
            bomStructureMap.get(v_superiorCode).getChild_List().add(bean);
            table.nextRow();
        }
        ArrayList<ProductBomStructure> bom_list=new ArrayList<ProductBomStructure>(bomStructureMap.values());
        Collections.sort(bom_list,new SortByProductBom_Code_Comp());
        return bom_list;
    }
}

/**
 * 对象排序
 */
class SortByProductBom_Code_Comp implements Comparator<ProductBomStructure> {
    @Override
    public int compare(ProductBomStructure o1, ProductBomStructure o2) {
        return o1.getProductCode().compareTo(o2.getProductCode());
    }
}